<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :lg="6" :xs="24">
        <div class="head-container" style="margin-bottom:10px;padding-left: 60px;">
          <span
            style="font-family: 'Arial Normal', 'Arial',serif;font-weight: bold;font-size: 20px;letter-spacing: normal;line-height: normal; font-style: normal;color: #333333;text-align: center;"
          >组织结构</span>
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            :default-expanded-keys="[100]"
            node-key="id"
            show-checkbox
            @check-change="handleCheckChange"
          >
          <span class="custom-tree-node" slot-scope="{ node, data }">
            <span v-if="data.label">
              <a class="treeTitle" :title="data.label">{{data.label | ellipsis}}</a>
            </span>
          </span>
        </el-tree>
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="18" :lg="18" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="168px"
        >
          <el-form-item label="获得积分或荣誉值时间：" prop="time">
            <el-date-picker
              v-model="time"
              align="right"
              type="daterange"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="change"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="statisticType">
            <el-select v-model="queryParams.examineType" placeholder="统计类型" clearable>
              <el-option
                v-for="dict in dict.type.examine_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">统计</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button
              type="danger"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
            >导出
            </el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="analysisList" style="margin-bottom: 30px" @sort-change="sortChange">
          <el-table-column label="排名" align="center" prop="rank" width="50"/>
          <el-table-column label="姓名" align="center" prop="userName"/>
          <el-table-column align="center" label="所属部门" prop="ancestorNames"/>
          <el-table-column align="center" label="获得总积分" prop="totalScore" sortable="custom">
            <template v-slot="scope">
              <el-link type="primary" :underline="false" @click="allScoreTable(scope.row.userId)">{{
                  scope.row.totalScore
                }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="获得总荣誉值" prop="totalHonorScore" sortable="custom">
            <template v-slot="scope">
              <el-link type="primary" :underline="false" @click="allhonorTable(scope.row.userId)">{{
                  scope.row.totalHonorScore
                }}
              </el-link>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
        <el-dialog title="积分记录" :visible.sync="dialogTableVisible" @queryTable="getExamineUserScore" append-to-body>
          <el-table :data="scoreRecord">
            <el-table-column align="center" label="时间" property="examineTime" width="250"/>
            <el-table-column property="score" label="积分" width="75" align="center"/>
            <el-table-column property="examineTypeAppend" label="内容" align="center"/>
          </el-table>
          <pagination
            v-show="totals>0"
            :total="totals"
            :page.sync="scoreQueryParams.pageNum"
            :limit.sync="scoreQueryParams.pageSize"
            @pagination="getExamineUserScore"
          />
        </el-dialog>
        <el-dialog title="荣誉值记录" :visible.sync="dialogTableVisible1" @queryTable="getExamineUserHonor"
                   append-to-body
        >
          <el-table :data="honorRecord">
            <el-table-column align="center" label="时间" property="examineTime" width="250"/>
            <el-table-column property="honorScore" label="荣誉值" width="75" align="center"/>
            <el-table-column property="examineTypeAppend" label="内容" align="center"/>
          </el-table>
          <pagination
            v-show="totalss>0"
            :total="totalss"
            :page.sync="honorQueryParams.pageNum"
            :limit.sync="honorQueryParams.pageSize"
            @pagination="getExamineUserHonor"
          />
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {treeselect} from '@/api/system/dept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {listAnalysis} from '@/api/examine/dataAnalysis/api'
import {listExamineUser} from '@/api/examine/user/api'

export default {
  filters:{
    ellipsis(value) {
      if (!value) return "";
      if (value.length > 10) {
        return value.slice(0, 10) + "...";
      }
      return value;
    }
  },
  name: 'Analysis',
  dicts: ['examine_type'],
  components: {
    Treeselect
  },
  data() {
    return {
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 部门树选项
      deptOptions: [],
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      deptPickList: [],
      analysisList: [],
      scoreRecord: [],
      honorRecord: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      totals: 0,
      totalss: 0,
      time: null,
      dialogTableVisible: false,
      dialogTableVisible1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        examineType: null,
        beginTime: null,
        finishTime: null,
        deptSearchStr: null,
        sortPropertyName: '',
        sortType: ''
      },
      scoreQueryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        examineType: null,
        beginTime: null,
        finishTime: null,
        userId: null,
        examineStatus: 2,
        isSubmitted: 1,
        orderByColumn: 'a.examine_time',
        isAsc: 'desc'
      },
      honorQueryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        examineType: null,
        beginTime: null,
        finishTime: null,
        userId: null,
        examineStatus: 2,
        isSubmitted: 1,
        orderByColumn: 'a.examine_time',
        isAsc: 'desc'
      }
    }
  },
  created() {
    this.getList()
    this.getTreeselect()
  },
  methods: {
    /** 查询数据分析列表 */
    getList() {
      this.loading = true
      listAnalysis(this.queryParams).then(response => {
        this.analysisList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getExamineUserScore() {
      listExamineUser(this.scoreQueryParams).then(response => {
        this.dialogTableVisible = true
        this.scoreRecord = response.rows
        this.totals = response.total
        this.loading = false
      })
    },
    getExamineUserHonor() {
      listExamineUser(this.honorQueryParams).then(response => {
        this.dialogTableVisible1 = true
        this.honorRecord = response.rows
        this.totalss = response.total
        this.loading = false
      })
    },
    sortChange(column) {
      this.queryParams.sortPropertyName = column.prop
      this.queryParams.sortType = column.order
      this.getList()
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data
        // this.handleNodeClick(response.data[0])
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // // 节点单击事件
    // handleNodeClick(data) {
    //   this.queryParams.deptId = data.id
    //   this.scoreQueryParams.deptId = data.id
    //   this.honorQueryParams.deptId = data.id
    //   this.handleQuery()
    // },
    // 节点单击事件
    handleCheckChange(data, checked) {
      //获取所有选中的节点id
      this.tenantIdMenus = this.$refs.tree.getCheckedNodes()
      this.deptPickList=this.tenantIdMenus
    },
    change(value) {
      if (value == null) {
        this.time = value
        this.queryParams.beginTime = value
        this.queryParams.finishTime = value
        this.scoreQueryParams.beginTime = value
        this.scoreQueryParams.finishTime = value
        this.honorQueryParams.beginTime = value
        this.honorQueryParams.finishTime = value
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.time) {
        this.queryParams.beginTime = this.time[0]
        this.queryParams.finishTime = this.time[1]
      }
      if (this.deptPickList) {
        this.queryParams.deptSearchStr = this.deptPickList.map(m => m.id).join(',')
      }
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 查询用户详细积分*/
    allScoreTable(userId) {
      this.scoreQueryParams.userId = userId
      this.scoreQueryParams.pageNum = 1
      this.scoreQueryParams.examineType = this.queryParams.examineType
      if (this.time) {
        this.scoreQueryParams.beginTime = this.time[0]
        this.scoreQueryParams.finishTime = this.time[1]
      }
      this.getExamineUserScore()
    },
    /**
     * 查询用户荣誉值
     * @param userId
     */
    allhonorTable(userId) {
      this.honorQueryParams.userId = userId
      this.honorQueryParams.pageNum = 1
      this.honorQueryParams.examineType = this.queryParams.examineType
      if (this.time) {
        this.honorQueryParams.beginTime = this.time[0]
        this.honorQueryParams.finishTime = this.time[1]
      }
      this.getExamineUserHonor()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.time = null
      this.queryParams.data = null
      this.queryParams.examineType = null
      this.queryParams.userId = null
      this.queryParams.deptSearchStr = null
      this.queryParams.beginTime = null
      this.queryParams.finishTime = null
      this.$refs.tree.setCheckedKeys([])
      this.deptPickList = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('examine/analysis/personalExport', {
        ...this.queryParams
      }, `个人激励排名.xlsx`)
    }
  }
}
</script>
